import React, { Component } from 'react';
import p1 from '../../images/k_pro5.png';
class View extends Component {
    constructor(props) {
        super(props);
        this.state = {
            time:''
        }
    }
    componentDidMount() {
        // 组件渲染之后调用，只调用一次。
        // ajax 和定时器使用
        // this.timerID = setInterval(() => {
        //     this.setState({
        //         time:new Date().toLocaleTimeString()
        //     })
        // },1000);
        const timeFn = () =>{
            this.setState({
                time:new Date().toLocaleTimeString()
            })
            setTimeout(timeFn,1000)
        }
        timeFn()
        this.timerID = setTimeout(timeFn,1000)

    }
    componentWillUnmount() {
        // #### 卸载
        // 组件将要卸载时调用，事件监听和定时器清楚
        clearTimeout(this.timerID);
        clearInterval(this.timerID);
    }
    render(){
        return (
            <React.Fragment>
                {this.state.time}
            </React.Fragment>
        )
    }
}
export default View;
